const config = require('../conf/app').imgServer;
const schema =  require('../conf/schema');
const { OK, ERR } = require('../util/util');
const ObjectID = require('mongodb').ObjectID;
const Formats = require('../util/formats');

const Image = schema.image;

const category = {
  init: function() {
    Image.count({}, function (err, count){
      console.log('total recored: ', count);
      if(count == 0) {
        const data = [{
          "favorited": false,
          "isLock": true,
          "name": "Pig",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e814d610d2494cd70c34019c07a03e88",
          "isNew": false,
          "price": 0,
          "isHot": false,
          "scenesImg": "",
          "id": "5a65a26da1a970000180df8e",
          "categoryId":"5b51b1ad5aba7af72c22a6e0",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": true,
          "name": "Trunk",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/72dc8b6141a620c4ecb82ca1bccd4ec0",
          "isNew": false,
          "price": 0,
          "isHot": false,
          "scenesImg": "",
          "id": "5a65a270a1a970000180e233",
          "categoryId":"5b51b1ad5aba7af72c22a6e1",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": false,
          "name": "House3",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/f229a1493d437361055be9aa4e9589e0",
          "isNew": false,
          "price": 0,
          "isHot": false,
          "scenesImg": "",
          "id": "5a925d80cacd48000184abbb",
          "categoryId":"5b51b1ad5aba7af72c22a6e0",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": false,
          "name": "Girl 6",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/9cae6643d6c5bbda84d35ca90e62cf98",
          "isNew": false,
          "price": 0,
          "isHot": false,
          "scenesImg": "",
          "id": "5a97d658cacd48000184abd9",
          "categoryId":"5b51b1ad5aba7af72c22a6e1",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": true,
          "name": "Parrot",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/8420f71292acb6422f2d51339e694c42",
          "isNew": false,
          "price": 5,
          "isHot": false,
          "scenesImg": "",
          "id": "5a65a26da1a970000180def6",
          "categoryId":"5b51b1ad5aba7af72c22a6e0",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": false,
          "name": "Ice Cube",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/aea981f59c9616e2baf062aa9ffff8bd",
          "isNew": false,
          "price": 3,
          "isHot": false,
          "scenesImg": "",
          "id": "5a3dec9d6e1c2d0001473b45",
          "categoryId":"5b51b1ad5aba7af72c22a6e9",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": true,
          "name": "Cat",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/00c55a1793c872c10edca62fee53432c",
          "isNew": false,
          "price": 5,
          "isHot": false,
          "scenesImg": "",
          "id": "5a65a26ea1a970000180dfe7",
          "categoryId":"5b51b1ad5aba7af72c22a6e0",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": true,
          "name": "Adorable Cat",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/6f3d2ae1b5e6848018d20c66febb243d",
          "isNew": false,
          "price": 5,
          "isHot": false,
          "scenesImg": "",
          "id": "5a65a26ca1a970000180ddda",
          "categoryId":"5b51b1ad5aba7af72c22a6e9",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": true,
          "name": "Camera",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/3f1997b5a10c3c24c299ac78ef09ec15",
          "isNew": false,
          "price": 5,
          "isHot": false,
          "scenesImg": "",
          "id": "5a65a26fa1a970000180e131",
          "categoryId":"5b51b1ad5aba7af72c22a6e9",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": false,
          "name": "Potted plants",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/55f1672a2442646bd4a6e66dec627b3d",
          "isNew": false,
          "price": 3,
          "isHot": false,
          "scenesImg": "",
          "id": "5a3dec426e1c2d0001473ab3",
          "categoryId":"5b51b1ad5aba7af72c22a6e9",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": false,
          "name": "Plants vs Zombies",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/448e59f959461c13e3a75cbb96a0db7b",
          "isNew": false,
          "price": 3,
          "isHot": false,
          "scenesImg": "",
          "id": "5a3dec436e1c2d0001473ae2",
          "categoryId":"5b51b1ad5aba7af72c22a6ed",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": true,
          "name": "Squirrel",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/aaf49dbed4068d65aea4957b72422495",
          "isNew": false,
          "price": 5,
          "isHot": false,
          "scenesImg": "",
          "id": "5a65a26da1a970000180df41",
          "categoryId":"5b51b1ad5aba7af72c22a6e0",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": true,
          "name": "Boy",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/437364cf303ae20f1c3cc34d6302e526",
          "isNew": false,
          "price": 5,
          "isHot": false,
          "scenesImg": "",
          "id": "5a65a26fa1a970000180e0fc",
          "categoryId":"5b51b1ad5aba7af72c22a6e0",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": true,
          "name": "Fish",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e6d7bb1fc78fa1e4856cdd5599688740",
          "isNew": false,
          "price": 5,
          "isHot": false,
          "scenesImg": "",
          "id": "5a65a26ca1a970000180de1e",
          "categoryId":"5b51b1ad5aba7af72c22a6e0",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": true,
          "name": "Heart",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/7b6f64bd82cd69de503eb94b4f1317c6",
          "isNew": false,
          "price": 5,
          "isHot": false,
          "scenesImg": "",
          "id": "5a65a26ca1a970000180de68",
          "categoryId":"5b51b1ad5aba7af72c22a6e0",
          "pos": 0,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "favorite_count": 0
        }, {
          "favorited": false,
          "isLock": true,
          "name": "Fruit Juice",
          "figure": "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e14ddd258e0c7146d49cb340208d64fe",
          "isNew": false,
          "price": 5,
          "isHot": false,
          "scenesImg": "",
          "id": "5a65a26fa1a970000180e13d",
          "pos": 1,
          "animationName" : "#Dinosaur_6.json",
          "colorImageList" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/e9ae631bee4ef9bf0aa7ea142a074978",
          "steps" : "0,50,125,200,300,425,550,650,750",
          "dashImg" : "https:\/\/easydrawing-lens.dailyinnovation.biz\/gallery\/27de75115277a106f657627a54aec620",
          "categoryId":"5b51b1ad5aba7af72c22a6ee",
          "favorite_count": 1
        }];

         console.log('init image: ', data);
         data.forEach(i => {
           const img = new Image({
             favorited: i.favorited,
             isLock: i.isLock,
             name: i.name,
             figure: i.figure,
             isnew: i.isNew,
             price: i.price,
             ishot: i.isHot,
             scenes_img: i.scenesImg,
             id: i.id,
             pos: i.pos,
             animationName : "#"+i.name+".json",
             colorImageList : i.colorImageList,
             steps : i.steps,
             dashImg : i.dashImg,
             categoryId: i.categoryId,
             favoriteCount: i.favoriteCount
           });
           img.save();
         });
       }
    });
  },
  findAll: function(limit, offset) {
    limit = limit || 16;
    offset = offset || 0;
    return Image.find({})
    .skip(parseInt(offset))
    .limit(parseInt(limit))
    .sort({id: -1})
    .exec(function (err, items) {
        if (err) {
            console.error(err);
            return ERR(500, err);
        } else {
          items.forEach(i=>{
            //console.log("item: ", i);
            //替换成本地图片
            var figure = config.local + i.name + '/';
            figure += i.figure.slice(-32)+".png";
            i.figure = figure;

            var dashImg = config.local + i.name + '/';
            dashImg += i.dashImg.slice(-32)+".png";
            i.dashImg = dashImg;

            
            var colorImageList = config.local + i.name + '/';
            colorImageList += i.colorImageList.slice(-32)+".png";
            i.colorImageList = colorImageList;
          });
          
          return items;
        }
    });
  },
  findByCategoryId: function(categoryId, limit, offset) {
       limit = limit || 16;
       offset = offset || 0;
    // return Image.find({categoryId:categoryId}, null, {sort: {id: -1}}, function (err, items) {
       return Image.find({categoryId:categoryId})
       .skip(parseInt(offset))
       .limit(parseInt(limit))
       .sort({id: -1})
       .exec(function (err, items) {
         if (err) {
           console.error(err);
           return ERR(500, err);
          } else {
            return items;
          }
        });
  },
  findById: function(id) {
       // return Image.findOne({_id:id})
       // .exec(function (err, items) {
       //   if (err) {
       //     console.error(err);
       //     return ERR(500, err);
       //    } else {
       //      return items;
       //    }
       //  });

        return Image.findOne({id : id}, function (err, re) {
            if (err) {
                return ERR(500, err);
            } else {
                if(re!=null) {
                    console.log("Fecth info: " + re );

                    return re;
                } else {
                  return ERR(500, err);
                }
            }
        });
  }
};

module.exports = category;
